package cn.tedu.service.impl;

import java.util.List;

import cn.tedu.dao.RoleDao;
import cn.tedu.dao.impl.RoleDaoImpl;
import cn.tedu.entity.Role;
import cn.tedu.service.RoleService;
import cn.tedu.util.PageUtil;
import cn.tedu.util.UUIDUtil;
import cn.tedu.vo.Page;
import cn.tedu.vo.Result;

/**
 * RoleService接口的子实现一
 * @author Administrator
 *
 */
public class RoleServiceImpl implements RoleService {
	RoleDao roleDao=new RoleDaoImpl();
	@Override
	public Result findRolesByPage(int currentPage, int pageSize, String rolekeyword) {
		Result result=new Result();
		try {
			Page<Role> page=new Page<Role>();
			page.setCurrentPage(currentPage);
			page.setPageSize(pageSize);
			rolekeyword="nochar".equals(rolekeyword)? "" : rolekeyword;
			page.setRolekeyword(rolekeyword);
			
			//查询数据库,获取指定模糊条件的总记录数
			int totalCount=roleDao.getCount(rolekeyword);
			page.setTotalCount(totalCount);
			
			//计算总页数
			int totalPage=(totalCount%pageSize==0)? (totalCount/pageSize) : (totalCount/pageSize)+1;
			page.setTotalPage(totalPage);
			
			//计算前一页
			if(currentPage==1) {
				page.setPreviousPage(1);
			}else {
				page.setPreviousPage(currentPage-1);
			}
			
			//计算后一页
			if(currentPage==totalPage) {
				page.setNextPage(totalPage);
			}else {
				page.setNextPage(currentPage+1);
			}
			
			//从数据库中获取当前页的那页数据
			List<Role> roles=roleDao.getRolesByPage(currentPage,pageSize,rolekeyword);
			page.setData(roles);
			
			//获取分页条中的超链接
			page.setaNum(PageUtil.getFenYe_a_Num(currentPage,pageSize,totalCount,totalPage));
			
			//能执行到此处,说明上面没有异常
			result.setStatus(1);
			result.setData(page);
		} catch (Exception e) {
			result.setStatus(0);
			result.setMessage("数据查询失败!!");
			e.printStackTrace();
		}
		return result;
	}
	@Override
	public Result addRole(String newRoleName) {
		Result result=new Result();
		try {
			Role role=new Role();
			role.setId(UUIDUtil.getUUID());
			role.setName(newRoleName);
			int rowAffect=roleDao.addRole(role);
			if(rowAffect==1) {
				result.setStatus(1);
				result.setMessage("添加角色成功!");
			}else {
				result.setStatus(0);
				result.setMessage("添加角色失败!");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	@Override
	public Result deleteRole(String roleId) {
		Result result=new Result();
		try {
			int rowAffect=roleDao.deleteRole(roleId);
			if(rowAffect==1) {
				result.setStatus(1);
				result.setMessage("删除角色成功!");
			}else {
				result.setStatus(0);
				result.setMessage("删除角色失败!!");
			}
		} catch (Exception e) {
			result.setStatus(0);
			result.setMessage("删除角色失败!!");
			e.printStackTrace();
		}
		return result;
	}
	@Override
	public Result updateRole(Role role) {
		Result result=new Result();
		try {
			int rowAffect=roleDao.updateRole(role);
			if(rowAffect==1) {
				result.setStatus(1);
				result.setMessage("更新角色成功!");
			}else {
				result.setStatus(0);
				result.setMessage("更新角色失败!!");
			}
		} catch (Exception e) {
			result.setStatus(0);
			result.setMessage("更新角色失败!!");
			e.printStackTrace();
		}
		return result;
	}
	@Override
	public Result findAllRoles() {
		Result result=new Result();
		try {
			List<Role> roles=roleDao.findAllRoles();
			result.setStatus(1);
			result.setData(roles);
		} catch (Exception e) {
			result.setStatus(0);
			result.setMessage("查询所有角色失败!!");
			e.printStackTrace();
		}		
		return result;
	}

}
